package com.mx.adapt.pager

import com.mx.adapt.MXBaseSimpleAdapt

abstract class MXBannerAdapt<T>(list: ArrayList<T> = arrayListOf()) : MXBaseSimpleAdapt<T>(list) {
    private var onLoopChangeCall: ILoopChange? = null
    private var isLoopMode = false

    fun isLoopMode(): Boolean = isLoopMode
    fun setLoopMode(loop: Boolean) {
        if (loop != isLoopMode) {
            isLoopMode = loop
            onLoopChangeCall?.onChange(loop)
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return if (isLoopMode()) Int.MAX_VALUE else super.getItemCount()
    }

    override fun getItem(position: Int): T? {
        val size = list.size
        if (size <= 0) return null
        return super.getItem(position % size)
    }

    internal fun setOnLoopChangeListener(listener: ILoopChange?) {
        onLoopChangeCall = listener
    }

    internal interface ILoopChange {
        fun onChange(loopMode: Boolean)
    }
}